package com.dobest.studentmanagement.web;

import com.dobest.studentmanagement.domain.Student;
import com.dobest.studentmanagement.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Package: com.dobest.studentmanagement.web
 * @author: Jounghu
 * @date: 2018/8/19 16:31
 * @version: V1.0
 */

@Controller
public class StuController {

    @Autowired
    StudentService studentService;

    @PostMapping("/addStu")
    public ModelAndView addStu(Student student, ModelAndView modelAndView) {
        Student stu = studentService.saveOrUpdate(student);
        modelAndView.addObject("stu",stu);
        modelAndView.addObject("msg", "添加成功");
        modelAndView.setViewName("redirect:index");
        return modelAndView;
    }

    @GetMapping("/search")
    public ModelAndView searchStu(String stuNum, ModelAndView modelAndView) {
        Student student = studentService.findOneByStuNum(stuNum);
        if (null == student) {
            modelAndView.setViewName("index");
            modelAndView.addObject("stu", new Student());
            modelAndView.addObject("msg", "没有找到该学生");
        } else {
            modelAndView.setViewName("index");
            modelAndView.addObject("stu", student);
            modelAndView.addObject("msg", "查找成功");
        }

        return modelAndView;
    }


}
